﻿using System.Drawing;
using System.Windows.Forms;
using System.Resources;
using System;
using System.IO;

namespace KoongTool
{
	public partial class Form1 : Form
	{
		const int _sellSize = 30;
		Point _fieldNum = new Point(5, 5);
		Point _selectPos = new Point(0, 0);
		SellType[,] _field = new SellType[13, 13];

		enum SellType
		{
			WHITE_SELL=0,
			BLUE_SELL,
			RED_SELL,
			YELLOW_SELL,
			GREEN_SELL,
			
			BLUE_BLOCK,
			RED_BLOCK,
			YELLOW_BLOCK,
			GREEN_BLOCK,

			LEFT_BLOCK,
			UP_BLOCK,
			RIGHT_BLOCK,
			DOWN_BLOCK,

			END_TYPE,
		};
		enum ShootType
		{
			UP_SHOOT = 0,
			DOWN_SHOOT,
			RIGHT_SHOOT,
			LEFT_SHOOT,
			END_SHHOT,
		};

		public Form1()
		{
			InitializeComponent();
		}

		private void _pictureBox_Paint(object sender, PaintEventArgs e)
		{
			Graphics g = e.Graphics;

			g.DrawImage(Image.FromFile(".//image/selectsell.png"), _selectPos.X * _sellSize, _selectPos.Y * _sellSize, _sellSize, _sellSize);

			for (int y = 0; y < _fieldNum.Y; ++y)
				for (int x = 0; x < _fieldNum.X; ++x)
				{
					g.DrawImage(getSellTypeImage(_field[x, y]), _sellSize * x, _sellSize * y, _sellSize, _sellSize);
				}
		}

		private Image getSellTypeImage(SellType st)
		{
			switch (st)
			{
				case SellType.WHITE_SELL: return Image.FromFile(".//image/sell.png");
				case SellType.BLUE_SELL: return Image.FromFile(".//image/bluesell.png");
				case SellType.RED_SELL: return Image.FromFile(".//image/redsell.png");
				case SellType.YELLOW_SELL: return Image.FromFile(".//image/yellowsell.png");
				case SellType.GREEN_SELL: return Image.FromFile(".//image/greensell.png");

				case SellType.BLUE_BLOCK: return Image.FromFile(".//image/colorblueblock.png");
				case SellType.RED_BLOCK: return Image.FromFile(".//image/colorredblock.png");
				case SellType.YELLOW_BLOCK: return Image.FromFile(".//image/coloryellowblock.png");
				case SellType.GREEN_BLOCK: return Image.FromFile(".//image/colorgreenblock.png");

				case SellType.LEFT_BLOCK: return Image.FromFile(".//image/wayleftblock.png");
				case SellType.UP_BLOCK: return Image.FromFile(".//image/wayupblock.png");
				case SellType.RIGHT_BLOCK: return Image.FromFile(".//image/wayrightblock.png");
				case SellType.DOWN_BLOCK: return Image.FromFile(".//image/waydownblock.png");
			};

			return Image.FromFile(".//image/sell.png");
		}

		private void numericUpDown1_ValueChanged(object sender, System.EventArgs e)
		{
			NumericUpDown nud = sender as NumericUpDown;
			_fieldNum.X = Convert.ToInt32(nud.Value);
			_fieldNum.Y = Convert.ToInt32(nud.Value);

			if (_selectPos.X >= _fieldNum.X) _selectPos.X = _fieldNum.X - 1;
			if (_selectPos.Y >= _fieldNum.Y) _selectPos.Y = _fieldNum.Y - 1;
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			_pictureBox.Refresh();

			label2.Text = "Cnt : " + shootListBox.Items.Count;
		}

		private void _pictureBox_MouseClick(object sender, MouseEventArgs e)
		{
			PictureBox pb = sender as PictureBox;
			//pb.PointToClient(new Point(Control.MousePosition.X, Control.MousePosition.Y));
			_selectPos.X = e.X / _sellSize;
			_selectPos.Y = e.Y / _sellSize;

			if (_selectPos.X >= _fieldNum.X) _selectPos.X = _fieldNum.X-1;
			if (_selectPos.Y >= _fieldNum.Y) _selectPos.Y = _fieldNum.Y-1;
		}

		private void buttonSellType_Click(object sender, EventArgs e)
		{
			Button b = sender as Button;

			switch (b.Name)
			{
				case "btnWhiteSell": _field[_selectPos.X, _selectPos.Y] = SellType.WHITE_SELL; break;
				case "btnBlueSell": _field[_selectPos.X, _selectPos.Y] = SellType.BLUE_SELL; break;
				case "btnRedSell": _field[_selectPos.X, _selectPos.Y] = SellType.RED_SELL; break;
				case "btnYellowSell": _field[_selectPos.X, _selectPos.Y] = SellType.YELLOW_SELL; break;
				case "btnGreenSell": _field[_selectPos.X, _selectPos.Y] = SellType.GREEN_SELL; break;

				case "btnBlueBlock": _field[_selectPos.X, _selectPos.Y] = SellType.BLUE_BLOCK; break;
				case "btnRedBlock": _field[_selectPos.X, _selectPos.Y] = SellType.RED_BLOCK; break;
				case "btnYellowBlock": _field[_selectPos.X, _selectPos.Y] = SellType.YELLOW_BLOCK; break;
				case "btnGreenBlock": _field[_selectPos.X, _selectPos.Y] = SellType.GREEN_BLOCK; break;
						 
				case "btnLeftBlock": _field[_selectPos.X, _selectPos.Y] = SellType.LEFT_BLOCK; break;
				case "btnUpBlock": _field[_selectPos.X, _selectPos.Y] = SellType.UP_BLOCK; break;
				case "btnRightBlock": _field[_selectPos.X, _selectPos.Y] = SellType.RIGHT_BLOCK; break;
				case "btnDownBlock": _field[_selectPos.X, _selectPos.Y] = SellType.DOWN_BLOCK; break;
			};
		}

		private void btnShootDel_Click(object sender, EventArgs e)
		{
			int selectIndex = shootListBox.SelectedIndex;

			if (selectIndex < 0) selectIndex = shootListBox.Items.Count-1;
			if( shootListBox.Items.Count > 0 )
				shootListBox.Items.RemoveAt(selectIndex);

		}

		private void btnShooWay_Click(object sender, EventArgs e)
		{
			Button b = sender as Button;

			switch (b.Text)
			{
				case "↓": shootListBox.Items.Add("↓"); break;
				case "↑": shootListBox.Items.Add("↑"); break;
				case "→": shootListBox.Items.Add("→"); break;
				case "←": shootListBox.Items.Add("←"); break;
			}
		}

		//입력자료 형태
		//버전 int
		//shoot 개수 int
		//shoot 입력 받음 - for + int
		//field X, Y크기 - int, int
		//field 입력 받음 - for + int

		private void 저장ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (saveFileDialog1.ShowDialog() == DialogResult.OK)
			{
				Stream stream = saveFileDialog1.OpenFile();
				if (stream != null)
				{
					BinaryWriter bw = new BinaryWriter(stream);

					int ver = 1;
					int shootCnt = shootListBox.Items.Count;

					bw.Write(ver); //버전
					bw.Write(shootCnt); //shoot 개수
					for(int i=0; i<shootCnt; ++i)//shoot 리스트 입력
						bw.Write( getShootStrToInt(shootListBox.Items[i].ToString()) );
					
					bw.Write(_fieldNum.X);
					bw.Write(_fieldNum.Y);//field의 x, y크기
					for(int y=0; y<_fieldNum.Y; ++y)//field 입력
						for (int x = 0; x < _fieldNum.X; ++x)
							bw.Write((int)_field[x, y]);

					bw.Close();
					stream.Close();
				}
			}
		}

		private void 열기ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				Stream stream = openFileDialog1.OpenFile();
				if (stream != null)
				{
					BinaryReader br = new BinaryReader(stream);

					int ver = 0;
					int shootCnt = 0;
					shootListBox.Items.Clear();

					ver = br.ReadInt32();
					shootCnt = br.ReadInt32();
					for (int i = 0; i < shootCnt; ++i)
						shootListBox.Items.Add(getShootIntToStr(br.ReadInt32()));

					_fieldNum.X = br.ReadInt32();
					_fieldNum.Y = br.ReadInt32();
					for (int y = 0; y < _fieldNum.Y; ++y)//field 입력
						for (int x = 0; x < _fieldNum.X; ++x)
							_field[x, y] = (SellType)br.ReadInt32();

					br.Close();
					stream.Close();
				}
			}
		}

		private int getShootStrToInt(string s)
		{
			//상 하 좌 우
			switch(s)
			{
				case "↓": return (int)ShootType.DOWN_SHOOT;
				case "↑": return (int)ShootType.UP_SHOOT;
				case "→": return (int)ShootType.RIGHT_SHOOT;
				case "←": return (int)ShootType.LEFT_SHOOT;
			}
			return (int)ShootType.END_SHHOT;
		}
		private String getShootIntToStr(int i)
		{
			//상 하 좌 우
			switch ((ShootType)i)
			{
				case ShootType.DOWN_SHOOT: return "↓";
				case ShootType.UP_SHOOT: return "↑";
				case ShootType.RIGHT_SHOOT: return "→";
				case ShootType.LEFT_SHOOT: return "←"; 
			}		
								 
			return "";		 
		}

			/*
			FileStream fs;
			if (!File.Exists("LinerBox")) //파일이 없으면
				fs = new FileStream("LinerBox", FileMode.CreateNew);//생성
			else
				fs = new FileStream("LinerBox", FileMode.Truncate); //리셋

			IFormatter bf = new BinaryFormatter();

			//암기 숫자 기억
			bf.Serialize(fs, countJob);

			foreach (KeyValuePair<String, Vocabulary> inVoca in vocaDtn)
			{
				Vocabulary tempAdd = inVoca.Value;
				//tempAdd.loadList_ = false; //초기때 계산하게 하기 위함.
				bf.Serialize(fs, tempAdd);
			}

			fs.Close();
			 */
	}
}
